<?php


class RedisFace extends FaceBase {

	private $nohf = array('');

	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		
		
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}

	}



	public function index(){
		//设置模式getOption
		//数据库数量dbSize
		//redis版本info
		//连接数ping
		//统计数据量
		//显示所有的数据项key*

		// type
		// 返回key的类型值
		// string: Redis::REDIS_STRING
		// set: Redis::REDIS_SET
		// list: Redis::REDIS_LIST
		// zset: Redis::REDIS_ZSET
		// hash: Redis::REDIS_HASH
		// other: Redis::REDIS_NOT_FOUND		

		$redisConf = n_confGet('redis');
		
		if(empty($redisConf['data'])){
			throw new PException("配置文件非法或未空");
		}

		$param = $this->param('key:char:ignore');

		$instances = array();


		foreach ($redisConf['data'] as $key => $val) {
			$instances[$key] = $val;
		}

		$key = $param['key'];
		if(empty($key)) {
			$key = array_shift(array_keys($instances));
		}
		
		
		$redis = n_api("redis")->$key;
		$keys = $redis->keys("*");
		$rs[$key]['conf'] = $instances[$key];
		$rs[$key]['dbSize'] = $redis->dbSize();
		$rs[$key]['info'] = $redis->info();
		$rs[$key]['ping'] = $redis->ping();
		//$rs[$key]['option'] = $redis->getOption();
		
		if(!empty($keys)){
			foreach ($keys as $key1 => $val1) {
				$type = $redis->type($val1);
				switch ($type) {
					case Redis::REDIS_STRING:$len = $redis->strlen($val1);$type='string';break;
					case Redis::REDIS_SET:$len = $redis->sSize($val1);$type='set';break;
					case Redis::REDIS_LIST:$len = $redis->llen($val1);$type='list';break;
					case Redis::REDIS_ZSET:$len = $redis->zSize($val1);$type='zset';break;
					case Redis::REDIS_HASH:$len = $redis->hLen($val1);$type='hash';break;
					
					default:
						$type = 'unknow';
						$len = 0;
						break;
				}
				$rs[$key]['items'][] = array("key"=>$val1,"type"=>$type,"len"=>$len);
				
			}
		}
		
		$rs['instances'] = $instances;
		$rs['key'] = $key;

 
			
		$this->_html("index",$rs);
		
	}

	public function showvar(){
		$param = $this->param("id:char");
		
		
		$redisConf = n_confGet('redis');
		
		if(empty($redisConf['data']) || empty($redisConf['data'][$param['id']])){
			throw new PException("配置文件非法或未空");
		}
		$id = $param['id'];
		$rHandle = n_api("redis")->$id;
		$rs = $rHandle->info();
		
		$this->_html("showvar",$rs);
	}
	
	
	public function itemData(){
		$param = $this->param("handle:char,id:char");
		
		$redisConf = n_confGet('redis');
		
		if(empty($redisConf['data']) || empty($redisConf['data'][$param['handle']])){
			throw new PException("配置文件非法或未空");
		}
		$handle = $param['handle'];
		$rHandle = n_api("redis")->$handle;


		$id = $param['id'];

		$type = $rHandle->type($id);

		switch ($type) {
			case Redis::REDIS_STRING:$len = $rHandle->strlen($id);$type='string';$data=$rHandle->get($id);break;
			case Redis::REDIS_SET:$len = $rHandle->sSize($id);$type='set';$data=$rHandle->sGetMembers($id);break;
			case Redis::REDIS_LIST:$len = $rHandle->llen($id);$type='list';$data=$rHandle->lGetRange($id,0,-1);break;
			case Redis::REDIS_ZSET:$len = $rHandle->zSize($id);$type='zset';$data=$rHandle->zRange($id,0,-1);break;
			case Redis::REDIS_HASH:$len = $rHandle->hLen($id);$type='hash';$data=$rHandle->hGetAll($id);break;
			
			default:
				$type = 'unknow';
				$len = 0;
				break;
		}
		$rs = array("key"=>$id,"type"=>$type,"len"=>$len,"data"=>$data);


		
		$this->_html("itemData",$rs);
	}
	public function itemDel(){

		$param = $this->param("handle:char,id:char");
		
		$redisConf = n_confGet('redis');
		
		if(empty($redisConf['data']) || empty($redisConf['data'][$param['handle']])){
			throw new PException("配置文件非法或未空");
		}
		$handle = $param['handle'];
		$rHandle = n_api("redis")->$handle;


		$id = $param['id'];
		$rHandle->delete($id);
		$this->_html("itemDel");
		
	}
	
	
	
	public function testLock(){
		echo '申请锁';
		if(n_api('redis')->c001->lock('test')){
			echo '申请成功';
		}else{
			echo '申请失败';
		}
		
	}
	public function testUnlock(){
		echo '申请开锁';
		if(n_api('redis')->c001->unlock('test')){
			echo '申请成功';
		}else{
			echo '申请失败';
		}
	}
	public function testHash(){
		for($i=0;$i<130;$i++){
			echo $i.":";
			var_dump(n_api("redis")->c001->hMget('kword',array($i)));
			echo "<br>";			
		}
	}
	public function __destruct(){
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->footer();
		}
	}	
}


?>